package demo3;

import java.util.*;

class ListNode{
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

public class Solution {

    //
    public int getDecimalValue(ListNode head) {
        ListNode cur = head;
        int ret = 0;
        while(cur != null){
            ret = ret * 2 + cur.val;
            cur = cur.next;
        }
        return ret;
    }

    //
    public static ListNode mergeNodes(ListNode head) {
        ListNode cur = new ListNode(-1);
        ListNode ccur = cur;
        ListNode dump = head;

        while(dump != null){
            if(dump.val == 0){
                int sum = 0;
                dump = dump.next;
                if(dump == null) { break;}
                while(dump.val != 0){
                    sum += dump.val;
                    dump = dump.next;
                }
                cur.next = new ListNode(sum);
                cur = cur.next;
            }
        }
        return ccur.next;
    }

    public static void main1(String[] args) {
        ListNode head = new ListNode(0);
        ListNode cur = head;
        cur.next = new ListNode(3);
        cur = cur.next;
        cur.next = new ListNode(1);
        cur = cur.next;
        cur.next = new ListNode(0);
        cur = cur.next;
        cur.next = new ListNode(4);
        cur = cur.next;
        cur.next = new ListNode(5);
        cur = cur.next;
        cur.next = new ListNode(2);
        cur = cur.next;
        cur.next = new ListNode(0);

        ListNode tmp = mergeNodes(head);
        System.out.println(tmp.val);
    }

    //
}
